package com.uzai.console.service.takebal.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.entity.WechatUser;
import com.uzai.common.enums.RedPackStatusEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.dto.takebal.redpacket.*;
import com.uzai.console.entity.RedPacket;
import com.uzai.console.mapper.UserTypeMapper;
import com.uzai.console.mapper.RedPacketMapper;
import com.uzai.console.service.takebal.RedPacketService;
import com.uzai.console.service.takebal.RedPacketSyncService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class RedPacketSyncServiceImpl implements RedPacketSyncService{

    Logger logger = LoggerFactory.getLogger(RedPacketSyncServiceImpl.class);

    @Autowired
    private RedPacketMapper redPacketMapper;
    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private RedPacketService redPacketService;

    /**
     * 发送筛选中的全部未领取红包
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketBatchSendMsgDto
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void  sendCardToUnReceivedSelectedSync(RedPacketBatchSendMsgDto redPacketBatchSendMsgDto){

        RedPacketQuery redPacketQuery = redPacketBatchSendMsgDto.getRedPacketQuery();
        redPacketQuery.setMerId(redPacketBatchSendMsgDto.getMerId());

        if(redPacketQuery.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //获取发送内容
        RedPacketSendMsgDto redPacketSendMsgDto = redPacketBatchSendMsgDto.getRedPacketSendMsgDto();
        if(redPacketSendMsgDto  == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //设置未领取状态
        redPacketQuery.setStatus(RedPackStatusEnum.UNCLAIMED.getValue());
        //循环查询条件
        Page<RedPacket> page = new Page<>();
        page.setCurrent(1);//只查第一页
        page.setSize(10); //每页查询10个
        page.addOrder(OrderItem.asc("id"));//默认id升序
        Long idStart = 0L;

        List<RedPacket> redPacketList = redPacketMapper.findByList(page, redPacketQuery);
        while (redPacketList != null && redPacketList.size() > 0){
            for (RedPacket redPacket : redPacketList){
                try{

                    if(redPacket.getId().longValue() > idStart.longValue()){
                        idStart = redPacket.getId();
                    }

                    if(redPacket.getStatus().intValue() == RedPackStatusEnum.UNCLAIMED.getValue().intValue()){
                        RedPacketSendMsgDto redPacketSendMsgDto_new = new RedPacketSendMsgDto();
                        redPacketSendMsgDto_new.setMerId(redPacketBatchSendMsgDto.getMerId());
                        redPacketSendMsgDto_new.setId(redPacket.getId());
                        redPacketSendMsgDto_new.setMessage(redPacketSendMsgDto.getMessage());
                        redPacketService.sendCardToOneRedPacketUnReceived(redPacketSendMsgDto_new);
                    }
                }catch (Exception e){
                    logger.error("-------sendCardToUnReceivedSelectedSync is error redpacket_id={}, error={}", redPacket.getId(), e.getMessage());
                }
            }

            redPacketQuery.setIdStart(idStart);
            redPacketList = redPacketMapper.findByList(page, redPacketQuery);

        }
    }

    /**
     * 发送筛选中的全部未领取红包
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketSelectedSendMsgDto
     * @return null
     */
    @Async("taskExecutor")
    @Override
    public void  sendCardToUnReceivedBatchSync(RedPacketSelectedSendMsgDto redPacketSelectedSendMsgDto){

        if(redPacketSelectedSendMsgDto.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        List<Long> idList = redPacketSelectedSendMsgDto.getIdList();

        if(idList == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择未领取红包记录");
        }

        if(idList != null && idList.size() > 0){
            for(Long id : idList){
                try{
                    RedPacketSendMsgDto redPacketSendMsgDto_new = new RedPacketSendMsgDto();
                    redPacketSendMsgDto_new.setMerId(redPacketSelectedSendMsgDto.getMerId());
                    redPacketSendMsgDto_new.setId(id);
                    redPacketSendMsgDto_new.setMessage(redPacketSelectedSendMsgDto.getMessage());
                    redPacketService.sendCardToOneRedPacketUnReceived(redPacketSendMsgDto_new);
                }catch (Exception e){
                    logger.error("-------sendCardToUnReceivedBatchSync is error redpacket_id={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 作废筛选中的全部未领取红包
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketQuery
     * @return null
     */
    @Async("taskExecutor")
    public void invalidALLUnReceived(RedPacketQuery redPacketQuery){
        if(redPacketQuery.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //设置未领取状态
        redPacketQuery.setStatus(RedPackStatusEnum.UNCLAIMED.getValue());
        //循环查询条件
        Page<RedPacket> page = new Page<>();
        page.setCurrent(1);//只查第一页
        page.setSize(10); //每页查询10个
        page.addOrder(OrderItem.asc("id"));//默认id升序
        Long idStart = 0L;

        List<RedPacket> redPacketList = redPacketMapper.findByList(page, redPacketQuery);
        while (redPacketList != null && redPacketList.size() > 0){
            for (RedPacket redPacket : redPacketList){
                try{

                    if(redPacket.getId().longValue() > idStart.longValue()){
                        idStart = redPacket.getId();
                    }

                    if(redPacket.getStatus().intValue() == RedPackStatusEnum.UNCLAIMED.getValue().intValue()){
                        RedPacketIdDto redPacketIdDto = new RedPacketIdDto();
                        redPacketIdDto.setMerId(redPacketQuery.getMerId());
                        redPacketIdDto.setId(redPacket.getId());
                        redPacketService.invalidToOneRedPacketUnReceived(redPacketIdDto);
                    }
                }catch (Exception e){
                    logger.error("-------invalidALLUnReceived is error redpacket_id={}, error={}", redPacket.getId(), e.getMessage());
                }
            }

            redPacketQuery.setIdStart(idStart);
            redPacketList = redPacketMapper.findByList(page, redPacketQuery);
        }
    }

    /**
     * 作废筛选中的全部未领取红包
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketQuery
     * @return null
     */
    @Async("taskExecutor")
    public void invalidAndRebackALLUnReceived(RedPacketQuery redPacketQuery){
        if(redPacketQuery.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        //设置未领取状态
        redPacketQuery.setStatus(RedPackStatusEnum.UNCLAIMED.getValue());
        //循环查询条件
        Page<RedPacket> page = new Page<>();
        page.setCurrent(1);//只查第一页
        page.setSize(10); //每页查询10个
        page.addOrder(OrderItem.asc("id"));//默认id升序
        Long idStart = 0L;

        List<RedPacket> redPacketList = redPacketMapper.findByList(page, redPacketQuery);
        while (redPacketList != null && redPacketList.size() > 0){
            for (RedPacket redPacket : redPacketList){
                try{
                    if(redPacket.getId().longValue() > idStart.longValue()){
                        idStart = redPacket.getId();
                    }

                    if(redPacket.getStatus().intValue() == RedPackStatusEnum.UNCLAIMED.getValue().intValue()){
                        RedPacketIdDto redPacketIdDto = new RedPacketIdDto();
                        redPacketIdDto.setMerId(redPacketQuery.getMerId());
                        redPacketIdDto.setId(redPacket.getId());
                        redPacketService.invalidAndRebackToOneRedPacketUnReceived(redPacketIdDto);
                    }
                }catch (Exception e){
                    logger.error("-------invalidAndRebackALLUnReceived is error redpacket_id={}, error={}", redPacket.getId(), e.getMessage());
                }
            }

            redPacketQuery.setIdStart(idStart);
            redPacketList = redPacketMapper.findByList(page, redPacketQuery);
        }
    }

    /**
     * 批量作废未领取红包(多选框)
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void invalidALLUnReceivedBatchSync(RedPacketIdListDto redPacketIdListDto){
        if(redPacketIdListDto.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //主键id列表
        List<Long> idList = redPacketIdListDto.getIdList();

        if(idList == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择作废的红包");
        }

        if(idList != null && idList.size() > 0){
            for (Long id : idList){
                try{
                    RedPacketIdDto redPacketIdDto = new RedPacketIdDto();
                    redPacketIdDto.setMerId(redPacketIdListDto.getMerId());
                    redPacketIdDto.setId(id);
                    redPacketService.invalidToOneRedPacketUnReceived(redPacketIdDto);
                }catch (Exception e){
                    logger.error("-------invalidALLUnReceived is error redpacket_id={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 批量作废并偿还未领取红包(多选框)
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void invalidAndRebackALLUnReceivedBatchSync(RedPacketIdListDto redPacketIdListDto){
        if(redPacketIdListDto.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //主键id列表
        List<Long> idList = redPacketIdListDto.getIdList();

        if(idList == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择作废的红包");
        }

        if(idList != null && idList.size() > 0){
            for (Long id : idList){
                try{
                    RedPacketIdDto redPacketIdDto = new RedPacketIdDto();
                    redPacketIdDto.setMerId(redPacketIdListDto.getMerId());
                    redPacketIdDto.setId(id);
                    redPacketService.invalidAndRebackToOneRedPacketUnReceived(redPacketIdDto);
                }catch (Exception e){
                    logger.error("-------invalidAndRebackALLUnReceived is error redpacket_id={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 批量发送消息(多选框)
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketSendBatchMsgDto
     * @return null
     */
    @Async("taskExecutor")
    public void sendMessageBatchSync(RedPacketSendBatchMsgDto redPacketSendBatchMsgDto){
        if(redPacketSendBatchMsgDto.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        if(StringUtils.isBlank(redPacketSendBatchMsgDto.getMessage())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入消息内容");
        }

        //主键id列表
        List<Long> idList = redPacketSendBatchMsgDto.getIdList();

        if(idList == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择发送消息的红包");
        }

        if(idList != null && idList.size() > 0){
            for (Long id : idList){
                try{
                    RedPacketSendMsgDto redPacketSendMsgDto = new RedPacketSendMsgDto();
                    redPacketSendMsgDto.setMerId(redPacketSendBatchMsgDto.getMerId());
                    redPacketSendMsgDto.setId(id);
                    redPacketSendMsgDto.setMessage(redPacketSendBatchMsgDto.getMessage());
                    redPacketService.sendMessage(redPacketSendMsgDto);
                }catch (Exception e){
                    logger.error("-------sendMessageBatchSync is error redpacket_id={}, error={}", id, e.getMessage());
                }
            }
        }
    }

    /**
     * 批量查询支付结果(多选框)
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param redPacketIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void queryResultBatchSync(RedPacketIdListDto redPacketIdListDto){
        if(redPacketIdListDto.getMerId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //主键id列表
        List<Long> idList = redPacketIdListDto.getIdList();

        if(idList == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择红包列表");
        }

        if(idList != null && idList.size() > 0){
            for (Long id : idList){
                try{
                    RedPacketIdDto redPacketIdDto = new RedPacketIdDto();
                    redPacketIdDto.setMerId(redPacketIdListDto.getMerId());
                    redPacketIdDto.setId(id);
                    redPacketService.queryResult(redPacketIdDto);
                }catch (Exception e){
                    logger.error("-------sendMessageBatchSync is error redpacket_id={}, error={}", id, e.getMessage());
                }
            }
        }
    }
}
